package logic

import (
	"context"
	"strconv"

	"gorm.io/gorm"

	"2106A-zg6/baidu/srv/internal/svc"
	"2106A-zg6/baidu/srv/models"
	"2106A-zg6/baidu/srv/pb/baidu"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChangeVipLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewChangeVipLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangeVipLogic {
	return &ChangeVipLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ChangeVipLogic) ChangeVip(in *baidu.ChangeVipRequest) (*baidu.ChangeVipResponse, error) {

	create, err := models.Create(&models.Order{
		OrderNo:     in.OrderNo,
		UserId:      in.UserId,
		MerchantId:  2,
		GoodsId:     "1",
		GoodsNum:    strconv.Itoa(2),
		TotalAmount: in.TotalAmount,
		Status:      1,
		PayType:     1,
		ShipAddress: "八维",
		Model:       gorm.Model{},
	}, in.OrderNo)
	if err != nil {
		return nil, err
	}

	return &baidu.ChangeVipResponse{
		OrderInfo: ModelRespOrder(create),
	}, nil
}

func ModelRespOrder(o models.Order) *baidu.Order {
	return &baidu.Order{
		Id:          int64(o.ID),
		OrderNo:     o.OrderNo,
		UserID:      o.UserId,
		MerchantID:  o.MerchantId,
		GoodID:      o.GoodsId,
		GoodNum:     o.GoodsNum,
		TotalAmount: o.TotalAmount,
		Status:      o.Status,
		PayType:     int64(o.PayType),
		ShipAddress: o.ShipAddress,
	}
}
